package com.iflysse.controller;


import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.resp.ClassBoResp;
import com.iflysse.pojo.bo.resp.ClassListBoResp;
import com.iflysse.pojo.bo.resp.TeachingClassNameBoResp;
import com.iflysse.pojo.bo.resp.TeachingClassNameListBoResp;
import com.iflysse.pojo.vo.resp.ClassListVoResp;
import com.iflysse.pojo.vo.resp.ClassResp;
import com.iflysse.services.ClassService;
import com.iflysse.services.TeacherClassService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/class")
@Api(value = "老师端班级接口")
@Log4j
public class ClassController {

    @Autowired
   private ClassService classService;
    @Autowired
   private TeacherClassService teacherClassService;

    /**
     * 查询所有的行政班级
     */
    @PostMapping("/queryAllAdministrativeClass")
    @ApiOperation("查询所有的行政班级")
    public ClassListVoResp doQueryAllAdministrativeClass() {

        ClassListVoResp classListVoResp=new ClassListVoResp();

        try {
            ClassListBoResp classListBoResp= classService.loadAllClasses();
            List<ClassBoResp> list= classListBoResp.getClassBoRespList();
            List<ClassResp>result=new ArrayList<ClassResp>();
            list.forEach(classBoResp -> {
                ClassResp classResp=new ClassResp();
                classResp.setClassName(classBoResp.getClassName());
                result.add(classResp);
            });
            classListVoResp.setResult(result);
            classListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            classListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教师端获取所有的行政班级列表的响应bean:"+classListVoResp);
            return classListVoResp;
        }catch (Exception e){
            log.info("教师端获取所有的行政班级列表出现异常，信息："+e);
            CommonUtil.printError(classListVoResp);
            return classListVoResp;
        }
    }


    /**
     * 老师端添加学生时
     * 获取所有的教学班级 下拉列表
     */
    @PostMapping("/queryAllTeachingClass")
    @ApiOperation("老师端添加学生时获取所有的教学班级得下拉列表")
    public ClassListVoResp doQueryAllTeachingClass() {

        ClassListVoResp classListVoResp=new ClassListVoResp();
        try{

            TeachingClassNameListBoResp teachingClassNameListBoResp= teacherClassService.queryAllTeacherClassNames();

            List<TeachingClassNameBoResp> teachingClassNameLists= teachingClassNameListBoResp.getTeachingClassNameBoRespList();
            List<ClassResp>result=new ArrayList<ClassResp>();
            teachingClassNameLists.forEach(teachingClassNameBoResp -> {
                ClassResp classResp=new ClassResp();
                classResp.setClassId(teachingClassNameBoResp.getTeachingClassId());
                classResp.setClassName(teachingClassNameBoResp.getTeachingClassName());
                result.add(classResp);
            });

            classListVoResp.setResult(result);
            classListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            classListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            log.info("老师端添加学生时获取所有的教学班级下拉列表响应bean:"+classListVoResp);
            return classListVoResp;
        }catch (Exception e){
            log.error("老师端添加学生时获取所有的教学班级下拉列表出现异常："+e);
            CommonUtil.printError(classListVoResp);
            return classListVoResp;
        }
    }
}
